package au.edu.unsw.infs3634.gamifiedlearning.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import au.edu.unsw.infs3634.gamifiedlearning.R;
import au.edu.unsw.infs3634.gamifiedlearning.model.ContinentModel;

public class ContinentsListAdapter extends RecyclerView.Adapter<ContinentsListAdapter.ViewHolder> {
    private Context context;
    private List<ContinentModel> datas;
    private OnItemClickListener listener;

    public ContinentsListAdapter(Context context, List<ContinentModel> datas, OnItemClickListener listener) {
        this.context = context;
        this.datas = datas;
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_continents,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ContinentModel model = datas.get(position);
        holder.tv.setText(model.getName());
        holder.img.setImageResource(model.getIcon());
        holder.root.setTag(model);
        holder.root.setOnClickListener(v->{
            ContinentModel _model = (ContinentModel) v.getTag();
            if (listener != null){
                listener.onItemClick(_model);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (datas == null){
            return 0;
        }
        return datas.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        private TextView tv;
        private ImageView img;
        private RelativeLayout root;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.tv);
            img = itemView.findViewById(R.id.img);
            root = itemView.findViewById(R.id.root);
        }
    }

    public interface OnItemClickListener{
        void onItemClick(ContinentModel model);
    }
}
